package com.minisoft.activity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.minisoft.customer.FlushCertData;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.junit.Test;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author JueSu
 * @description: TODO
 * @date 2022/9/19 13:10
 */
@Slf4j
public class ActivityExport {

    @Test
    public void exportAnalysisData() throws Exception {
        ExcelWriter writer = ExcelUtil.getWriter("D:\\temp\\活动报表0109.xlsx");

        int rowCount = writer.getRowCount();
        for (int i = 1; i < rowCount; i++) {
            JSONObject rowJson = FlushCertData.getRowJson(writer, i);
            String template = rowJson.getStr("活动模板");
            Integer id = rowJson.getInt("id");
            if (!NumberUtil.isNumber(template) || id == null) {
                continue;
            }

            log.info("{}/{},actId = {}",i,rowCount,id);
            JSONObject body = JSONUtil.createObj().set("id",id);
            String s = HttpUtil.post("http://activity.paas.huafagroup.com/admin-api/v1/promotion-activity/analysisDetail?createdBy=chenjuesu&userId=000049599&positionId=00000009", body.toString());
            log.info(s);
            JSONObject data = JSONUtil.parseObj(s).getJSONObject("data");
            JSONArray overviewOfIndicators = data.getJSONArray("overviewOfIndicators");
            if (overviewOfIndicators == null) {
                continue;
            }
            Map<String,Integer> overviewOfIndicatorsMap = new HashMap<>();
            for (int j = 0; j < overviewOfIndicators.size(); j++) {
                JSONObject item = overviewOfIndicators.getJSONObject(j);
                overviewOfIndicatorsMap.put(item.getStr("indexPath"),item.getInt("totalNumber"));
            }
            int rowIdx = i,colIdx = 5;
            writer.writeCellValue(2,rowIdx,data.getStr("template"));
            writer.writeCellValue(colIdx++, rowIdx, data.get("visitPeopleNumber"))
                    .writeCellValue(colIdx++, rowIdx, data.get("goVisitPeopleNumber"))
                    .writeCellValue(colIdx++, rowIdx, data.get("dealPeopleNumber"))
                    .writeCellValue(colIdx++, rowIdx, data.get("goDealPeopleNumber"))
                    .writeCellValue(colIdx++, rowIdx, overviewOfIndicatorsMap.get("活动访问人数"))
                    .writeCellValue(colIdx++, rowIdx, overviewOfIndicatorsMap.get("活动参与人数"))
                    .writeCellValue(colIdx++, rowIdx, overviewOfIndicatorsMap.get("活动访问次数"));
            writer.flush();
        }
        writer.close();
    }
}
